package com.koron.order.risk.base.bean.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.BiMap;
import com.koron.order.risk.base.bean.po.*;
import com.koron.order.risk.base.bean.vo.LineRiskVO;
import com.koron.order.risk.util.LineRiskConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/12/15
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(value="风险管控单详情页", description = "LineRiskDTO")
@Slf4j
public class LineRiskDTO extends LineRiskVO {

    @ApiModelProperty(value = "管控措施列表")
    private List<LineRiskMeasure> lineRiskMeasureList;

    @ApiModelProperty(value = "风险详情-违章建筑")
    private LineRiskDetailBuilding lineRiskDetailBuilding;

    @ApiModelProperty(value = "风险详情-施工作业区")
    private LineRiskDetailConstruction lineRiskDetailConstruction;

    @ApiModelProperty(value = "风险详情-默认")
    private LineRiskDetailDefault lineRiskDetailDefault;

    @ApiModelProperty(value = "风险消除确认信息")
    private LineRiskEliminate lineRiskEliminate;

    public List<LineRiskMeasure> getLineRiskMeasureList() {
        if (CollUtil.isEmpty(lineRiskMeasureList)) {
            return lineRiskMeasureList;
        }
        long controlMeasuresCount = lineRiskMeasureList.stream().map(LineRiskMeasure::getControlMeasures).distinct().count();
        Assert.isTrue(lineRiskMeasureList.size() == controlMeasuresCount, "管控措施类型不允许重复");
        return lineRiskMeasureList;
    }

    public String getLineRiskMeasureStr(BiMap<String, String> controlMeasuresMap) {
        if (CollUtil.isEmpty(lineRiskMeasureList)) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < lineRiskMeasureList.size(); i++) {
            LineRiskMeasure lineRiskMeasure = lineRiskMeasureList.get(i);
            String controlMeasures = lineRiskMeasure.getControlMeasures();
            Integer implemented = lineRiskMeasure.getImplemented();
            String line = "";
            try {
                line = (i + 1) + "、" + controlMeasuresMap.get(controlMeasures) + ":" + (Objects.equals(implemented, 1) ? "已落实" : "未落实");
            } catch (Exception e) {
                log.error("第{}个数据报错，{}", i ,e.getStackTrace());
            }
            result.append(line).append("\n");
        }
        return result.toString();
    }
    /**
     * 计算落实状态
     * 0 未落实
     * 1 落实中
     * 2 已落实
     *
     * 落实状态：只有风险管控单状态为“风险管控中” 才显示落实状态，此状态为管控管控措施中的落实状态
     * （1）已落实：管控措施均已落实，
     * 如管控措施中存在看护值守这个措施，则需要判断对应的看护值守工单是否正在值守，
     * 如未在值守，则即便有值守工单也不能算是已落实；
     * （2）落实中：管控措施中的部分措施落实；
     * （3）未落实：管控措施中的措施均未落实；
     */
    public String computeWorkableStatus() {
        if (LineRiskConstant.Status.getStatusWeight(getStatus()) < 2 || CollUtil.isEmpty(lineRiskMeasureList)) {
            return null;
        }
        int count = lineRiskMeasureList.stream().map(LineRiskMeasure::getNoNanImplemented).reduce(Integer::sum).orElse(0);
        if (count == 0) {
            return "0";
        }
        else if (count == lineRiskMeasureList.size()) {
            return "2";
        }
        return "1";
    }

    /**
     * 负责人的审批账号，给工作流使用的
     * @return 逗号分割
     */
    public String getLeaderIdStr() {
        List<LineRiskMeasure> lineRiskMeasureList = getLineRiskMeasureList();
        if (CollUtil.isEmpty(lineRiskMeasureList)) {
            return "";
        }
        return lineRiskMeasureList.stream().map(LineRiskMeasure::getLeaderId).collect(Collectors.joining(","));
    }
}
